<!DOCTYPE html>
<html lang="en" xmlns:sa="http://www.thymeleaf.org/extras/sa-token"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .layui-upload-img {
            width: 92px;
            height: 92px;
        }
    </style>
    <!--公共模块-->
    <div th:replace="~{layout}"></div>
</head>
<body>
<div class="panel panel-default operation" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="info">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="title" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <select id="type" name="type" lay-filter="select-type"
                            th:with="type=${@sysDictService.getType('content_type')}">
                        <option value="0">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">关键字</label>
                <div class="layui-input-block" th:with="type=${@sysDictService.getType('content_keyword')}">
                    <input class="disabled" th:each="dict : ${type}" type="checkbox"
                           th:name="'keywords[' + ${dict.value} + ']'"
                           th:title="${dict.label}"></input>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    <textarea id="content" name="content"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table_div">
    <div id="searchParam" sa:hasPermission="sysContent:list">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="title" class="layui-input" autocomplete="off" placeholder="请输入标题">
                </div>

                <div class="layui-input-inline ">
                    <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </form>
    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" sa:hasPermission="sysContent:add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" sa:hasPermission="sysContent:delete">删除
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="preview" sa:hasPermission="sysContent:list">预览</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" sa:hasPermission="sysContent:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" sa:hasPermission="sysContent:delete">删除</a>
</script>

</body>
</html>
<script>
    //文章类型字典数据
    var datas = "[[${@sysDictService.getType('content_type')}]]".replace(/&quot;/g, "\"");

    layui.config({
        base: ctx + 'static/layui-ext/'
    }).extend({
        tinymce: 'tinymce/tinymce',
    }).use(['tinymce'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        //建立富文本编辑器
        var tinymce = layui.tinymce
        var edit = tinymce.render({
            elem: "#content"
            , height: 500
            , width: '80%'
            , images_upload_url: ctx + 'sysFiles/upload'
            , form: {
                name: 'file'//配置上传文件的字段名称
            }
        });

        //加载table
        var tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , page: true //开启分页
            , url: ctx + 'sysContent/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'title', title: '标题', sort: true},
                    {
                        field: 'type', title: '类型', sort: true, templet: function (item) {
                            //获取类型对应的字典label
                            return CoreUtil.selectDictLabel(datas, item.type);
                        }
                    },
                    {field: 'content', title: '内容', sort: true},
                    {field: 'createTime', title: '创建时间', sort: true},
                    {width: 200, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        //表头工具
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的列");
                    } else {
                        var ids = [];
                        $(data).each(function (index, item) {
                            ids.push(item.id);
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("新增");
                    form.val('info', {
                        "id": "",
                        "title": "",
                        "type": "0"
                    })
                    edit.setContent('')
                    break;
            }
            ;
        });
        //列操作
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var ids = [];
                    ids.push(data.id);
                    tipDialog(ids);
                    break;
                case 'edit':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("编辑");
                    form.val('info', {
                        "id": data.id,
                        "title": data.title,
                        "type": data.type
                    })
                    edit.setContent(data.content)
                    //复选框回显
                    form.val('info', CoreUtil.setCheckboxValues("keywords", data.keywords));
                    break;
                case 'preview':
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: "预览",
                        shadeClose: true, //开启遮罩关闭
                        area: ['900px', '500px'], //宽高
                        shade: 0.6, //遮罩透明度
                        maxmin: true, //允许全屏最小化
                        anim: 1, //0-6的动画形式，-1不开启
                        content: data.content, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                        cancel: function () {
                        }
                    });
                    break;
            }
        });


        //删除
        var tipDialog = function (ids) {
            layer.open({
                content: "确定要删除么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete(ctx + "sysContent/delete", ids, function (res) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            tableIns1.reload();
                        });
                    })
                }
            });
        };

        //返回
        $("#btn_cancel").click(function () {
            $(".table_div").show();
            $(".operation").hide();
            return false;
        });

        //监听保存
        form.on('submit(submit)', function (data) {
            //获取复选框的值
            data.field.keywords = CoreUtil.getCheckboxValues("keywords", data.field);

            data.field.content = edit.getContent();
            //图文
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost(ctx + "sysContent/add", data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    tableIns1.reload();
                })
            } else {
                CoreUtil.sendPut(ctx + "sysContent/update", data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    tableIns1.reload();
                })
            }

            return false;
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            tableIns1.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });

    });
</script>